//
//  AppDelegate.swift
//  WeiBoApp
//
//  Created by James on 2017/6/11.
//  Copyright © 2017年 James. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WBMainViewController()
        window?.makeKeyAndVisible()
        
        print(NSHomeDirectory())
//        loadAppInfo()
        
        setUpAdditons(application: application)
        
        return true
    }
}
// MARK: -设置应用程序额外信息
extension AppDelegate {
    fileprivate func setUpAdditons(application: UIApplication){
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        //1.消失时间
        
        //2.设置网络加载指示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //3.获取用户授权  通知 角标  等等
        // #available 是检查设备版本，如果是10.0以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.carPlay,.sound]){( success,error ) in
                print("授权结果" + (success ? "成功" : "失败"))
            }
        } else {
            //            10.0以下
            let notificationSetting = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            
            application.registerUserNotificationSettings(notificationSetting)
        }
    }
}

extension AppDelegate {

    fileprivate func loadAppInfo(){
        //1.模拟异步
        DispatchQueue.main.async {
            //1>url
            let url = Bundle.main.path(forResource: "1.json", ofType: nil)
            let data = NSData(contentsOfFile: url!)
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            let jsonPath = (docDir as NSString).appendingPathComponent("main.json")
            
            //写入本地、
            data?.write(toFile: jsonPath, atomically: true)
            print("程序记载完毕\(jsonPath)")
            
            
        }
    
    }
}
